<script setup>
import { reactive, ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import loginApi from '../api/login_api.js'
import { User, Lock, Key } from '@element-plus/icons-vue'
import { ArrowRight } from '@element-plus/icons-vue'

const user = ref({
  username: '',
  password: '',
  captcha: '',
  key: ''
});

onMounted(function(){
  localStorage.removeItem("token");
  // 加载验证码
  loadCaptcha();
})

// 获取路由对象
const router = useRouter();
const route = useRoute();

// 验证码图片数据
const captcha = ref("");

const onSubmit = async () => {
  if (!user.value.username || !user.value.password || !user.value.captcha) {
    ElMessage.warning('请填写完整信息');
    return;
  }

  try {
    let res;
    // 根据路由路径决定调用哪个登录接口
    if (route.path === '/admin-login') {
      // 管理员登录
      res = await loginApi.adminLogin(user.value);
    } else {
      // 用户登录
      res = await loginApi.login(user.value);
    }

    if (res) {
      localStorage.setItem('token', res);
      // 跳转到对应首页
      if (route.path === '/admin-login') {
        router.push('/admin/adminIndex'); // 管理员登录后跳转到管理员首页
      } else {
        router.push('/user/shopping'); // 用户登录后跳转到用户首页
      }
    }
  } catch (error) {
    ElMessage.error(error.message || '登录失败');
    await loadCaptcha(); // 重新加载验证码
  }
}

const onRegister = async () => {
  await router.push('/register');
}

// 跳转到忘记密码页面
const onForgotPassword = () => {
  router.push('/forgot-password');
}

// 加载验证码
const loadCaptcha = async () => {
  try {
    const res = await loginApi.captcha();
    user.value.key = res.key;
    captcha.value = res.base;
  } catch (error) {
    ElMessage.error('验证码加载失败');
  }
}

// 切换登录类型
const switchLoginType = () => {
  if (route.path === '/admin-login') {
    router.push('/login');
  } else {
    router.push('/admin-login');
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <img src="../assets/logo.png" alt="Logo" class="logo"/>
        <h1>{{ route.path === '/admin-login' ? '管理员登录' : '用户登录' }}</h1>
      </div>

      <el-form :model="user" class="login-form">
        <el-form-item>
          <el-input
              v-model="user.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
              maxlength="30"
              size="large"
              class="login-input"
          />
        </el-form-item>

        <el-form-item>
          <el-input
              v-model="user.password"
              placeholder="请输入密码"
              show-password
              :prefix-icon="Lock"
              maxlength="30"
              size="large"
              class="login-input"
          />
        </el-form-item>

        <el-form-item class="captcha-item">
          <el-input
              v-model="user.captcha"
              placeholder="验证码"
              :prefix-icon="Key"
              maxlength="5"
              size="large"
              class="captcha-input"
          />
          <img
              :src="captcha"
              class="captcha-image"
              @click="loadCaptcha"
              alt="验证码"
          />
        </el-form-item>

        <el-form-item>
          <el-button
              class="login-button"
              type="primary"
              @click="onSubmit"
              size="large"
          >
            {{ route.path === '/admin-login' ? '管理员登录' : '用户登录' }}
            <el-icon class="login-icon">
              <ArrowRight/>
            </el-icon>
          </el-button>
        </el-form-item>

        <div class="switch-login">
          <span>{{ route.path === '/admin-login' ? '普通用户?' : '管理员?' }}</span>
          <el-button
              type="text"
              @click="switchLoginType"
              class="switch-button"
          >
            {{ route.path === '/admin-login' ? '用户登录' : '管理员登录' }}
          </el-button>
        </div>

        <div v-if="route.path !== '/admin-login'" class="register-link">
          <span>还没有账户？</span>
          <el-button
              type="text"
              @click="onRegister"
              class="register-button"
          >
            立即注册
          </el-button>
        </div>

        <div v-if="route.path !== '/admin-login'" class="login-actions">
          <el-button
              type="text"
              @click="onForgotPassword"
              class="forgot-password-button"
          >
            忘记密码?
          </el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  min-height: 100vh;
  background-image: url('../assets/background.png');
  background-size: cover;
  background-position: center;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 400px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
  border: 1px solid #e1edff;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 15px;
}

.login-header h1 {
  color: #333;
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.login-form {
  margin-top: 20px;
}

.login-input {
  width: 100%;
}

:deep(.login-input .el-input__wrapper) {
  background: #ffffff;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  box-shadow: none;
}

:deep(.login-input .el-input__inner) {
  color: #606266;
  background: transparent;
}

:deep(.login-input .el-input__inner::placeholder) {
  color: #c0c4cc;
}

.captcha-item {
  display: flex;
  gap: 10px;
  align-items: center;
}

.captcha-input {
  flex: 1;
}

.captcha-image {
  width: 100px;
  height: 40px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  cursor: pointer;
  background: #fff;
}

.login-button {
  width: 100%;
  background: linear-gradient(90deg, #0e88eb 0%, #1a9fff 100%);
  border: none;
  font-weight: 600;
  letter-spacing: 1px;
  margin-top: 10px;
  color: white;
}

.login-button:hover {
  background: linear-gradient(90deg, #1a9fff 0%, #0e88eb 100%);
  transform: translateY(-2px);
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px rgba(14, 136, 235, 0.3);
}

.login-icon {
  margin-left: 5px;
  animation: slideIn 1s ease infinite;
}

@keyframes slideIn {
  0%, 100% {
    transform: translateX(0);
  }
  50% {
    transform: translateX(3px);
  }
}

.login-actions, .switch-login {
  display: flex;
  justify-content: space-between;
  margin-top: 15px;
}

.register-link, .switch-login {
  text-align: center;
  margin-top: 20px;
  color: #666;
  font-size: 14px;
}

.forgot-password-button,
.register-button,
.switch-button {
  color: #0e88eb;
  font-weight: 600;
  padding: 0;
}

.forgot-password-button:hover,
.register-button:hover,
.switch-button:hover {
  color: #1a9fff;
  text-decoration: underline;
}

@media (max-width: 480px) {
  .login-box {
    padding: 20px;
  }

  .captcha-item {
    flex-direction: column;
    align-items: stretch;
  }

  .captcha-image {
    width: 100%;
    margin-top: 10px;
  }

  .login-actions, .switch-login {
    flex-direction: column;
    gap: 10px;
    align-items: center;
  }
}
</style>
